

import com.sun.org.apache.xerces.internal.util.HTTPInputSource;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: marina
 * Date: 14.02.13
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");

        req.getRequestDispatcher("/form.ftl").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String sDate = req.getParameter("date1");
        String sCity = req.getParameter("city");
        int nights;

        try {
            Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(sDate);
            Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("date2"));

            long millis = date2.getTime() - date1.getTime();

            nights = (int) (millis / (1000*24*60*60));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        URL url = new URL(String.format("http://xmlfeed.laterooms.com/index.aspx?aid=11939&rtype=4&atype=true&compressed=true&kword=%s&lang=en&sdate=%s&nights=%s&cur=USD", sCity, sDate, nights));

        Root root;

        try (InputStream in = url.openStream()) {
            root = (Root) JAXBContext.newInstance(Root.class).createUnmarshaller().unmarshal(in);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }

      /*  PrintWriter writer = resp.getWriter();

        writer.print("<html>");
        writer.print(" <head>");
        writer.print("  <meta charset='utf-8'>");
        writer.print("<link href='//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css' rel='stylesheet'>");
        writer.print(" </head>");
        writer.print(" <body>");
        writer.print("<div class='container'>");

        for (Hotel hotel : root.getHotels()) {
            writer.print(String.format("<h4><a href='%s'>%s</a></h4><br />", hotel.getHotelLink(), hotel.getName()));
            writer.print(String.format("<img class='img-rounded img-polaroid' src='%s' style='height:200px;' /> <br /> <hr />", hotel.getImage()));
        }

        writer.print("</div>");
        writer.println("</body></html>");   */
        req.setAttribute("hotels", root.getHotels());

        req.getRequestDispatcher("/results.ftl").forward(req, resp);
    }
}
